/**
 * 角色详情对话框
 */
var DeptInfoDlg = {
    data: {
        pid: "",
        pName: ""
    }
};

layui.use(['layer', 'form', 'admin', 'ax'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var layer = layui.layer;

    /**
     * 应收会计
     */
    var languageAjax = new $ax(Feng.ctxPath + "/area/userList");
    var languageResult = languageAjax.start();
    $.each(languageResult, function( index, val ) {
        $("#yskj").append("<option value='"+val.userId+"'>"+val.name+"</option>");
    } );
    form.render('select');

    //应付会计
    // var languageAjax1 = new $ax(Feng.ctxPath + "/area/userList");
    // var languageResult1 = languageAjax.start();
    $.each(languageResult, function( index, val ) {
        $("#yfkj").append("<option value='"+val.userId+"'>"+val.name+"</option>");
    } );
    form.render('select');

    //成本会计
    // var languageAjax2 = new $ax(Feng.ctxPath + "/area/userList");
    // var languageResult2 = languageAjax.start();
    $.each(languageResult, function( index, val ) {
        $("#cbkj").append("<option value='"+val.userId+"'>"+val.name+"</option>");
    } );
    form.render('select');


    //获取区域信息
    var ajax = new $ax(Feng.ctxPath + "/area/detail/" + Feng.getUrlParam("id"));
    var result = ajax.start();
    form.val('deptForm', result);


    // 点击上级角色时
    // $('#pName').click(function () {
    //     var formName = encodeURIComponent("parent.DeptInfoDlg.data.pName");
    //     var formId = encodeURIComponent("parent.DeptInfoDlg.data.pid");
    //     var treeUrl = encodeURIComponent("/area/tree");
    //
    //     layer.open({
    //         type: 2,
    //         title: '父级区域',
    //         area: ['300px', '400px'],
    //         content: Feng.ctxPath + '/system/commonTree?formName=' + formName + "&formId=" + formId + "&treeUrl=" + treeUrl,
    //         end: function () {
    //             $("#pid").val(DeptInfoDlg.data.pid);
    //             $("#pName").val(DeptInfoDlg.data.pName);
    //         }
    //     });
    // });

    // 表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/area/update", function (data) {
            if(data.success){
                Feng.success("修改成功！");
            }else{
                Feng.error(data.message);
                return;
            }

            //传给上个页面，刷新table用
            admin.putTempData('formOk', true);

            //关掉对话框
            admin.closeThisDialog();

        }, function (data) {
            Feng.error("修改失败！" + data.responseJSON.message)
        });

        var yskjId = $("#yskj").find("option:selected").val();
        var yskj = $("#yskj").find("option:selected").text() == '选择应收会计' ? '' : $("#yskj").find("option:selected").text();
        var yfkjId = $("#yfkj").find("option:selected").val();
        var yfkj = $("#yfkj").find("option:selected").text() == '选择应付会计' ? '' : $("#yfkj").find("option:selected").text();
        var cbkjId = $("#cbkj").find("option:selected").val();
        var cbkj = $("#cbkj").find("option:selected").text() == '选择成本会计' ? '' : $("#cbkj").find("option:selected").text();
        data.field.yskjId = yskjId;
        data.field.yskj = yskj;
        data.field.yfkjId = yfkjId;
        data.field.yfkj = yfkj;
        data.field.cbkjId = cbkjId;
        data.field.cbkj = cbkj;

        ajax.set(data.field);
        ajax.start();

        //添加 return false 可成功跳转页面
        return false;
    });

});